package liuds.music.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;


import java.util.List;

import liuds.music.R;

/**
 * Created by Administrator on 2018/7/10.
 */

public abstract class DropSelectAdapter<T> extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;
    private List<T> list;
    private int selectedIndex = -1;
    private T selectedItem;
    private View.OnClickListener onClickListener;

    private OnChangeListener<T> onChangeListener;

    public DropSelectAdapter(Context context, List<T> list){
        this.context = context;
        this.list = list;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.drop_select_item,null);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(onClickListener!=null){
                    onClickListener.onClick(v);
                }
            }
        });
        return new DropSelectViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        DropSelectViewHolder dropSelectViewHolder = (DropSelectViewHolder)holder;
        dropSelectViewHolder.title.setText(getItemTitle(list.get(position),position)==null?getTitle(list.get(position),position):getItemTitle(list.get(position),position));

        dropSelectViewHolder.selected.setVisibility(selectedIndex == position?View.VISIBLE:View.INVISIBLE);
    }

    public abstract String getTitle(T item,int position);

    public String getItemTitle(T item,int position){
        return null;
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    private class DropSelectViewHolder extends RecyclerView.ViewHolder{

        private ImageView selected;
        private TextView title;

        private DropSelectViewHolder(View itemView) {
            super(itemView);
            title = itemView.findViewById(R.id.title);
            selected = itemView.findViewById(R.id.selected);
        }
    }

    public List<T> getList() {
        return list;
    }

    public void setList(List<T> list) {
        this.list = list;
    }

    public T getItem(int position){
        return list.get(position);
    }

    public int getSelectedIndex() {
        return selectedIndex;
    }

    public T getSelectedItem() {
        return selectedItem;
    }

    public void setSelected(int position){
        this.selectedIndex = position;
        this.selectedItem = getItem(position);
        this.notifyDataSetChanged();
        if(onChangeListener!=null){
            onChangeListener.onChange(list.get(position),position);
        }
    }

    public void clearSelected(){
        this.selectedIndex = -1;
        this.selectedItem = null;
        this.notifyDataSetChanged();
    }

    public void setOnClickListener(View.OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    public void setOnChangeListener(OnChangeListener<T> onChangeListener) {
        this.onChangeListener = onChangeListener;
    }

    public interface OnChangeListener<T>{
        void onChange(T selectedItem, int position);
    }
}